package prototipo.handler;

import java.io.IOException;
import java.net.InetAddress;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MainClassHandler extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(MainClassHandler.class.getName());
	private static int cantEventosRecibidos = 0;
	private int contador = 0;
	

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		postear(resp, "En doGet()");
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		postear(resp, "En doPost() de MainClassHandler");
		cantEventosRecibidos++;
		postear(resp, "Recibio un evento, es el numero: "+cantEventosRecibidos);
		InetAddress p = InetAddress.getLocalHost();
		postear(resp, "Canonical host:" + p.getCanonicalHostName());
		resp.setContentType("text/html");
		contador = 0;
	}
	
	private void postear(HttpServletResponse rest, String mensaje) throws IOException {
		rest.getOutputStream().print(contador + ") " + mensaje + "</br>");
		log.info(contador + ")" + mensaje);
		contador++;
	}


}
